<template>
	<view class="case-batch-manage">
		<scroll-view class="case-batch-manage-list" scroll-y>
			<checkbox-group v-model="choseList" @change="change">
				<view class="case-batch-manage-item" v-for="(data,index) in List" :key="index">
					<label>
						<checkbox :value="(data.case_id).toString()" :checked="data.checked" color="#FFF" />
						<image :src="data.image" mode="aspectFit"></image>
						<view class="case-manage-item-info">
							<view class="case-manage-item-tit">{{data.case_name}}</view>
							<view class="case-manage-item-style">{{data.style_name}}</view>
							<view class="case-manage-item-tag">
								<text>人气：{{data.click_mun}}</text>
								<text>收藏：{{data.case_collect_num}}</text>
							</view>
						</view>
					</label>
				</view>
			</checkbox-group>
			<no-more :text="'数据加载完毕'" v-if="List.length && noMore"></no-more>
		</scroll-view>
		<view class="case-batch-manage-bottom">
			<checkbox-group @change="allChose">
				<label><checkbox color="#FFF" :checked="isAll" value="all"/>{{isAll?'取消全选':'全选'}}</label>
			</checkbox-group>
			<view @click="del" class="del">删除案例</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				List: [],
				choseList: [],
				allChecked: [],
				isAll: false,
				style_id: '',
				page: 0,
				noMore: false
			};
		},
		onLoad(options) {
			if(options.style_id){
				this.style_id = options.style_id
				uni.setNavigationBarTitle({
					title: '案例管理'
				})
			}
		},
		onShow() {
			if(this.style_id){
				this.init(0,this.style_id)
			}else{
				this.init(0)
			}
		},
		methods: {
			init(page,style_id){
				let data = {
					order_by: 'add_time',
					asc: 1,
					page: page?page:0,
					limit: 10,
					style_id: style_id?style_id:'',
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id
				}
				this.$api.CaseList(data).then(res => {
					if(res.datas.list){
						if(res.datas.list.length){
							res.datas.list.forEach(item => {
								this.List.push(item)
							})
						}
					}
				})
			},
			change(e){
				let _this = this
				var items = this.List,
						values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if(values.includes(item.case_id.toString())){
						this.$set(item,'checked',true)
					}else{
						this.$set(item,'checked',false)
					}
				}
				let arr = []
				this.List.forEach((item,index) => {
					if(item.checked){
						arr.push(item.id)
					}
				})
				if(arr.length==this.List.length){
					this.isAll = true
				}else{
					this.isAll = false
				}
			},
			allChose(e){
				if(e.detail.value.length){
					this.isAll = true
					this.List.forEach((item,index) => {
						this.$set(item,'checked',true)
					})
				}else{
					this.isAll = false
					this.List.forEach((item,index) => {
						this.$set(item,'checked',false)
					})
				}
			},
			del(){
				let select = [],
						_this = this;
				this.List.forEach(item => {
					if(item.checked){
						select.push(item.case_id)
					}
				})
				if(!select.length){
					uni.showToast({
						icon: "none",
						title: "请选择需要删除的案例"
					})
					return
				}
				uni.showModal({
					content: '是否确认删除选择的案例?',
					success(res) {
						if(res.confirm){
							_this.$api.DelateCase({case_id: select.join(',')}).then(res => {
								uni.showToast({
									icon: "none",
									title: "删除成功",
									duration: 1500
								})
								setTimeout(() => {
									_this.List = []
									if(_this.style_id){
										_this.init(0,_this.style_id)
									}else{
										_this.init(0)
									}
								},1500)
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import  '../../static/scss/case-batch-manage.scss'
</style>